package com.rytong.tools.ui.chart;

import java.util.ArrayList;
import java.util.List;

import com.rytong.tools.ui.Component;
import com.rytong.tools.ui.CssStyle;
import com.rytong.tools.ui.LPLayout;
import com.rytong.tools.ui.LPScrollView;
import com.rytong.tools.ui.LPLayout.MyLPLayout;
import com.rytong.tools.utils.Utils;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable.Orientation;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.ViewGroup;
import android.widget.AbsoluteLayout;
import android.widget.AbsoluteLayout.LayoutParams;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;

/**
 * @author zhou.yang 图标控件
 */
public class LPPlotLayout extends Component {

    // x轴的marginTop
    float xScaleMaginTop_;
    // y轴的marginTop
    float yScaleMaginTop_;
    // y轴的刻度值集合
    List<String> yValueSpace_;
    
    public class MyLPPlotLayout extends AbsoluteLayout implements CompositedComponent {

        public MyLPPlotLayout(Context context, String value) {
            super(context);
        }

        @Override
        public Component composited() {
            return LPPlotLayout.this;
        }

        @Override
        public boolean onTouchEvent(MotionEvent event) {
            switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
            	// 按下tr时候变色。
            	trDownColor(tr_, llvSV_);
                xDown_ = event.getX();
                yDown_ = event.getY();
                break;
            case MotionEvent.ACTION_MOVE:
            	// 移动tr恢复颜色。
            	int offset = (int) Math.abs(yDown_ - event.getY());
            	if (offset > offset_) {
            		trMoveColor(tr_, llvSV_);
            	}
                moveY(yDown_, event.getY());
                break;
            }
            return super.onTouchEvent(event);
        }

    }

    @Override
    public void initRealView(Activity activity, String nodeValue) {
        super.initRealView(activity, nodeValue);
        realView_ = new MyLPPlotLayout(activity, nodeValue);
        childrenList_ = new ArrayList<Component>(5);
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.rytong.tools.ui.Component#mould()
     */
    @SuppressWarnings("deprecation")
    @Override
    public void mouldH() throws Exception {
        super.mouldH();
        mouldChildList();

        // 1.计算x，y轴的布局参数
        int size = childrenList_.size();
        LPXScale scaleX = null;
        LPYScale scaleY = null;
        Component child = null;
        String childType = null;
        for (int i = 0; i < size; i++) {
            child = childrenList_.get(i);
            childType = String.valueOf(child.getPropertyByName("type"));
            if (childType != null && childType.equals("xscale")) {
                scaleX = (LPXScale) child;
            } else if (childType != null && childType.equals("yscale")) {
                scaleY = (LPYScale) child;
            }
        }

        // 设置y轴的LayoutParams
        LayoutParams lpYScale = (LayoutParams) scaleY.getLayoutParams();
        lpYScale.width = scaleY.yScaleWidth_;
        scaleY.width_ = scaleY.yScaleWidth_;
        lpYScale.height = height_;
        scaleY.height_ = height_;
        lpYScale.x = 0;
        lpYScale.y = 0;
        scaleY.setLayoutParams(lpYScale);

        CustomSeekBar seekBar = null;
        int seekbarHeight = 0;
        String selectedType = (String) property_.get("selectedType");
        boolean needSeekBar = false;
        // 判断是否加滑动条
        if (selectedType != null && selectedType.trim().equals("1")) {
            needSeekBar = true;
        }
        if (needSeekBar) {
            // 创建滑动条
            seekBar = new CustomSeekBar(getContext());
            seekbarHeight = height_ / 10;
            // 滑块的宽度为其高度的3/4
            seekBar.sliderWidth_ = (seekbarHeight * 3) >> 2;
        }

        // 设置x轴的LayoutParams
        LayoutParams lpXScale = (LayoutParams) scaleX.getLayoutParams();
        lpXScale.height = scaleX.height_;
        lpXScale.width = scaleX.width_;
        lpXScale.y = height_ - scaleX.height_ - seekbarHeight;
        scaleX.setLayoutParams(lpXScale);

        if (needSeekBar) {
            // 设置滑动条的输出参数
            int seekbarWidth = width_ - scaleY.width_ + (seekBar.sliderWidth_ >> 1);
            LayoutParams seekBarParams = new LayoutParams(seekbarWidth, seekbarHeight, 0, 0);
            seekBarParams.x = scaleY.width_ - (seekBar.sliderWidth_ >> 1);
            seekBarParams.y = height_ - seekbarHeight;
            seekBar.setLayoutParams(seekBarParams);
        }

        // 计算y轴的显示高度
        scaleY.displayHeight_ = height_ - scaleX.height_ - seekbarHeight;

        // 把x轴和视图布局加到scrollView中
        HorizontalScrollView horizontalScrollView = new HorizontalScrollView(getContext());
        android.widget.AbsoluteLayout.LayoutParams lp = new android.widget.AbsoluteLayout.LayoutParams(width_
                - scaleY.width_ - scaleY.SPACE_LEFT - scaleY.SPACE_RIGHT, scaleX.height_ + scaleY.displayHeight_,
                scaleY.width_, 0);
        horizontalScrollView.setLayoutParams(lp);
        // 设置滚动条不显示
        horizontalScrollView.setHorizontalScrollBarEnabled(false);
        AbsoluteLayout absoluteLayout = new AbsoluteLayout(getContext());
        absoluteLayout.setLayoutParams(new LayoutParams(width_ - scaleY.width_, scaleX.height_ + scaleY.displayHeight_,
                0, 0));

        // 设置绘图区域的输出参数
        LayoutParams chartParams = new LayoutParams(scaleX.width_, scaleY.displayHeight_, 0, 0);

        // 创建背景布局视图
        int color = Color.parseColor("#00000000");
        cssStyle_ = cssStyle();
        if (cssStyle_ != null) {
            String linecolor = (String) cssStyle_.getProperty(CssStyle.COLOR);
            if (null != linecolor && !linecolor.equals("")) {
                color = Utils.getColorValue(linecolor);
            }
        }
        LPBackgroundLayout lpBackgroundLayout = new LPBackgroundLayout(getContext(), scaleX.size_, scaleY.fontHeight_,
                false, color);
        lpBackgroundLayout.setLayoutParams(chartParams);
        lpBackgroundLayout.backgroundLayoutWidth_ = scaleX.width_;
        lpBackgroundLayout.backgroundLayoutHeight_ = scaleY.displayHeight_;
        lpBackgroundLayout.setXScaleMarginTop(scaleX.valueSpace_);
        lpBackgroundLayout.setYScaleMarginTop(scaleY.valueSpace_);
        lpBackgroundLayout.setYScaleValues(scaleY.lineValue_);
        absoluteLayout.addView(lpBackgroundLayout);

        Component lastChart = null;
        boolean needLine = false;
        for (int i = 0; i < size; i++) {
            child = childrenList_.get(i);
            if (child instanceof Chart) {
                child.height_ = scaleY.displayHeight_;
                child.width_ = scaleX.width_;
                ((Chart) child).setXScaleMarginTop(scaleX.valueSpace_);
                ((Chart) child).setYScaleMarginTop(scaleY.valueSpace_);
                ((Chart) child).setYScaleValues(scaleY.lineValue_);
                ((Chart) child).setXScaleValues(scaleX.lineValue_);
                child.mouldW();
                child.mouldH();
                child.realView_.setLayoutParams(chartParams);
                absoluteLayout.addView(child.realView_);
                if (i == size - 1) {
                	if (child instanceof LPLinePlotLayout || child instanceof LPColumnPlotLayout) {
                        needLine = true;
                    }
                    lastChart = child;
                }
            }
        }
        absoluteLayout.addView(scaleX.realView_);
        // 如果最上层的图表是折线图就显示垂直线
        if (needSeekBar) {
            seekBar.setChartAndScrollView(lastChart, horizontalScrollView);
            if (needLine) {
                // 创建和seekbar关联的画线view
                LPBackgroundLayout lineView = new LPBackgroundLayout(getContext(), scaleX.size_, scaleY.fontHeight_,
                        true, color);
                lineView.setLayoutParams(chartParams);
                lineView.backgroundLayoutWidth_ = scaleX.width_;
                lineView.backgroundLayoutHeight_ = scaleY.displayHeight_;
                lineView.setXScaleMarginTop(scaleX.valueSpace_);
                lineView.setYScaleMarginTop(scaleY.valueSpace_);
                lineView.setYScaleValues(scaleY.lineValue_);
                absoluteLayout.addView(lineView);
                seekBar.lineView_ = lineView;
            }
        }

        horizontalScrollView.addView(absoluteLayout);

        // 2.组装x，y轴及图表控件到plotLayout中
        AbsoluteLayout plotLayout = (AbsoluteLayout) realView_;
        plotLayout.removeAllViews();

        plotLayout.addView(scaleY.realView_);
        plotLayout.addView(horizontalScrollView);
        if (needSeekBar) {
            plotLayout.addView(seekBar);
        }
    }
    
    @Override
    public void releaseResource(Component component) {
    	super.releaseResource(component);
    	if(yValueSpace_ != null){
    		yValueSpace_.clear();
    		yValueSpace_ = null;
    	}
    }
}
